﻿using ABCNet.Authorization.Users;
using ABCNet.Extensions;
using ABCNet.PlatformFinancialLists;
using ABCNet.PlatformInfos.Dto;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABCNet.PlatformInfos
{
    public class PlatformInfoAppService : ABCNetAppServiceBase
    {
        private readonly IRepository<PlatformInfo> _repository;
        private readonly IRepository<PlatformFinancialList> _financialRepository;
        private readonly IRepository<User, long> _userRepository;
        
        public PlatformInfoAppService(IRepository<PlatformInfo> repository,
            IRepository<PlatformFinancialList> financialRepository,
            IRepository<User, long> userRepository)
        {
            _repository = repository;
            _financialRepository = financialRepository;
            _userRepository = userRepository;
        }


        /// <summary>
        /// 查询平台收入明细分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<PlatformFinancialListDto>> GetPlatformFinancialPageList(FinancialQueryInput input)
        {
            var query = from fin in _financialRepository.GetAll()
                            .WhereIf(input.BusinessType != null && input.BusinessType.HasValue, x => x.BusinessType == input.BusinessType.Value)
                            .WhereIf(input.StartDate != null && input.StartDate.HasValue, x => x.CreationTime >= input.StartDate.Value.Date)
                            .WhereIf(input.EndDate != null && input.EndDate.HasValue, x => x.CreationTime < input.EndDate.Value.Date.AddDays(1))
                        join user in _userRepository.GetAll()
                            .WhereIf(!string.IsNullOrWhiteSpace(input.UserName), x => x.UserName.Contains(input.UserName.Trim())) 
                            on fin.UserId equals user.Id
                        orderby fin.CreationTime descending
                        select new PlatformFinancialListDto
                        {
                            Id = fin.Id,
                            AfterChangedAmount = fin.AfterChangedAmount,
                            BeforeChangedAmount = fin.BeforeChangedAmount,
                            ChangedAmount = fin.ChangedAmount,
                            BusinessId = fin.BusinessId,
                            BusinessType = fin.BusinessType,
                            BusinessTypeName = fin.BusinessTypeName,
                            CreationTime = fin.CreationTime,
                            CreatorUserId = fin.CreatorUserId,
                            NetPointId = fin.NetPointId,
                            Remark = fin.Remark,
                            UserId = fin.UserId,
                            UserName = user.UserName
                        };

            //总行数
            var totalCount = await query.CountAsync();

            int startIndex = (input.StartPage - 1) * input.PageCount;
            query = query.Skip(startIndex).Take(input.PageCount);
            //获取对象
            var rlst = await query.ToListAsync();

            //组装分页返回对象
            var PagedResult = new PagedResultDto<PlatformFinancialListDto>(totalCount, rlst);
            return PagedResult;
        }

    }
}
